package br.ucb.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import br.ucb.bean.Login;
import br.ucb.dao.LoginDAO;
import br.ucb.util.SimpleEntityManager;

@ManagedBean
@SessionScoped
public class LoginManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7539201643898217378L;

	private Login login;
	private List<Login> logins;
	private SimpleEntityManager manager;
	private LoginDAO loginDAO;
	
	public LoginManagedBean(){
		this.login = new Login();
		this.manager = new SimpleEntityManager("sacop");
		this.loginDAO = new LoginDAO(this.manager.getEntityManager());
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public List<Login> getLogins() {
		return logins;
	}

	public void setLogins(List<Login> logins) {
		this.logins = logins;
	}

	public void findAllLogins() {
		this.setLogins(this.loginDAO.listar());
	}

	// setar strigs depois que a pagina for criada
	public String salvar() {
		loginDAO.salvar(getLogin());
		return "--------------";
	}

	public String deleteLogin(Login l) {
		loginDAO.delete(l);
		return "-------------";
	}

	public String setObjectForUpdate(Login l) {
		setLogin(l);
		return "-------------";
	}

	public String alteraLogin() {
		this.loginDAO.update(getLogin());
		return "------------";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((logins == null) ? 0 : logins.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LoginManagedBean other = (LoginManagedBean) obj;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (logins == null) {
			if (other.logins != null)
				return false;
		} else if (!logins.equals(other.logins))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "LoginManagedBean [login=" + login + ", logins=" + logins + "]";
	}

}
